package com.chrray.position.service.position.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chrray.position.auth.Authentication;
import com.chrray.position.auth.AuthenticationContext;
import com.chrray.position.enums.app.FdUserRoleEnum;
import com.chrray.position.exception.ServiceException;
import com.chrray.position.mapper.position.FdHomeAdMapper;
import com.chrray.position.service.position.FdHomeAdService;
import lombok.AllArgsConstructor;
import com.chrray.position.domain.FdHomeAd;
import com.chrray.position.domain.vo.FdHomeAdVO;
import com.chrray.position.domain.dto.search.FdHomeAdSearch;
import com.chrray.position.domain.dto.FdHomeAdDTO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.BeanUtils;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 首页广告配置表 接口实现类
 *
 * @author wusiwee
 * @email wusiwee@163.com
 * @since 1.0.1 2024-08-07
 */
@Service
@AllArgsConstructor
public class FdHomeAdServiceImpl extends ServiceImpl<FdHomeAdMapper, FdHomeAd> implements FdHomeAdService {

    @Override
    public FdHomeAdVO latestAd() {
        FdHomeAdVO vo = new FdHomeAdVO();
        Authentication context = AuthenticationContext.context();
        Integer currentRole = context.getCurrentRole();
        FdHomeAd entity = new FdHomeAd().findLatestOne(currentRole);
        if (entity != null) {
            BeanUtils.copyProperties(entity, vo);
        }
        return vo;
    }


    /**
     * 分页查询
     * @param param 查询条件
     * @return 分页列表
     */
    @Override
    public IPage<FdHomeAdVO> pageList(FdHomeAdSearch param) {
        //查询数据
        IPage<FdHomeAd> page = page(getPage(param), getWrapper(param));
        IPage<FdHomeAdVO> voPage = new Page<>();
        //数据组装
        voPage.setRecords(page.getRecords().stream().map(r->{
            FdHomeAdVO vo = new FdHomeAdVO();
            BeanUtils.copyProperties(r,vo);
            //数据组装
            setViewParam(r,vo);
            return vo;
        }).collect(Collectors.toList()));
        voPage.setPages(page.getPages());
        voPage.setTotal(page.getTotal());
        voPage.setSize(page.getSize());
        voPage.setCurrent(page.getCurrent());
        return voPage;
    }

    /**
     * 动态条件构建
     * @param param 查询参数
     * @return 条件语句
     */
    private LambdaQueryWrapper<FdHomeAd> getWrapper(FdHomeAdSearch param){
        LambdaQueryWrapper<FdHomeAd> wrapper = Wrappers.lambdaQuery();
        // 主键ID
        wrapper.eq(param.getId() != null, FdHomeAd::getId, param.getId());
        // 名称
        wrapper.eq(param.getName() != null && !param.getName().isEmpty(), FdHomeAd::getName, param.getName());
        // 位置: 0-雇工首页, 1-雇主首页
        wrapper.eq(param.getPosition() != null, FdHomeAd::getPosition, param.getPosition());
        // 图片地址
        wrapper.eq(param.getImageUrl() != null && !param.getImageUrl().isEmpty(), FdHomeAd::getImageUrl, param.getImageUrl());
        // 状态
        wrapper.eq(param.getStatus() != null, FdHomeAd::getStatus, param.getStatus());
        //
        wrapper.eq(param.getDelFlag() != null, FdHomeAd::getDelFlag, param.getDelFlag());
        // 主键倒序
        wrapper.orderByDesc(FdHomeAd::getId);
        return wrapper;
    }

    /**
     * 详情
     * @param id 查询条件
     * @return 详情
    */
    @Override
    public FdHomeAdVO info(Long id) {
        // 查询主键详情
        FdHomeAd entity = getById(id);
        if (ObjectUtils.isEmpty(entity)){
            throw new ServiceException("查询不到数据");
        }
        // 回显参数封装
        FdHomeAdVO result = new FdHomeAdVO();
        BeanUtils.copyProperties(entity,result);
        // 数据组装
        setViewParam(entity,result);
        return result;
    }

    /**
     * 新增
     * @param body 参数
     * @return 成功与否
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveOrUpdate(FdHomeAdDTO body) {
        if (body.getId() == null){
            // 主键为空
            FdHomeAd entity = new FdHomeAd();
            BeanUtils.copyProperties(body,entity);
            // 新增
            return save(entity);
        }else {
            // 主键不为空
            FdHomeAd entity = getById(body.getId());
            BeanUtils.copyProperties(body,entity);
            // 更新
            return updateById(entity);
        }
    }

    /**
     * 批量删除
     * @param idList 主键列表
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> idList) {
        //逻辑删除
        //FdHomeAd entity = new FdHomeAd();
        //entity.setDelFlag(1);
        //return update(entity,new LambdaQueryWrapper<FdHomeAd>().in(FdHomeAd::getId,idList));

        //物理删除
        return removeByIds(idList);
    }


    /**
     * 获取分页对象
     *
     * @param param 分页参数
     */
    protected IPage<FdHomeAd> getPage(FdHomeAdSearch param) {
        return new Page<>(param.getPageNum(), param.getPageSize());
    }

    /**
     * 视图数据赋值
     * @param r 数据
     * @param vo 视图
     */
    private void setViewParam(FdHomeAd r, FdHomeAdVO vo) {
        //数据赋值业务..
    }

}
